home *** CD-ROM | disk | FTP | other *** search
- /*-------------------- METAFILE.C --------------------------
- *
- * Author: Gary Parker
- * Last update: 3-12-91
- *
- * Modified by: Al Watters
- * Date: 6-11-91
- * Modifications:
- * line changed from : #if defined(_TURBOC_) Turbo C
- * to : #if defined(__BORLANDC__) Borland C
- * compiles and runs OK under Borland C++ 2.0
- *
- * Compilers: Borland C++ 2.0, Turbo C++, Turbo C 2.0, MSC 6.0
- * Memory model: any
- *
- * A demonstration program for metaphone().
- * The program will search a drive for a filename
- * which sounds similar to the name passed.
- *
- * This code is hereby placed in the public domain.
- *-----------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dos.h>
-
- #if defined(__BORLANDC__) /* Borland C */
- #include <dir.h>
- typedef struct ffblk FILEINFO;
- #define F_FIRST(a,b,c) findfirst(a,c,b)
- #define F_NEXT findnext
- #define F_ATTRIB ff_attrib
- #define F_DATE ff_fdate
- #define F_SIZE ff_fsize
- #define F_NAME ff_name
- #else
- typedef struct find_t FILEINFO; /* MSC */
- #define F_FIRST _dos_findfirst
- #define F_NEXT _dos_findnext
- #define F_ATTRIB attrib
- #define F_DATE wr_fdate
- #define F_SIZE size
- #define F_NAME name
- #endif
-
- /* file attributes */
- #define RDONLY 0x01 /* Read only file */
- #define HIDDEN 0x02 /* Hidden file */
- #define SYSTEM 0x04 /* System file */
- #define SUBDIR 0x10 /* Subdirectory */
- #define ARCH 0x20 /* Archive file */
-
- /* display the file and path if it matches */
- void print_find_t(char *dir, FILEINFO *find);
-
- /* perform metaphone comparison */
- int metaphone(char *name, char *m, int first);
-
- /* search dos file system recursively */
- void search(char *dir, char *name);
-
- int found = 0;
- char meta[8]; /* storage for first metaph */
-
- main(int argc, char *argv[])
- {
- char *ptr;
-
- if(argc == 1) {
- puts("syntax METAFILE [filename.ext]");
- exit(1);
- }
- /* remove extension if any */
- if((ptr = memchr(argv[1], '.', 9)) != 0) *ptr = 0;
- metaphone(argv[1], meta, 1); /* store the metaph */
- printf("\nmetaph for %s is %s\n", argv[1], meta);
- search("", ""); /* search for matchs */
- if(!found) printf("A match was not found\n");
- return 0;
- }
-
- void search(char *dir, char *newdir)
- {
- char curdir[80];
- FILEINFO find;
- char *ptr;
-
- strcpy(curdir, dir); /* build first pathname */
- strcat(curdir, newdir);
- strcat(curdir, "\\*.*");
-
- /* check first file */
- if( !F_FIRST( curdir, 0xFF, &find)) {
- curdir[strlen(curdir) - 3] = 0; /* remove *.* */
- do {
- if(find.F_NAME[0] == '.') continue;
- else { /* skip the extension */
- if((ptr = memchr(find.F_NAME, '.', 9)) != 0)
- *ptr = 0;
-
- /* call metaphone with flag set to
- 0 to compare string stored metaph */
-
- if(metaphone(find.F_NAME, meta, 0) == 0) {
- /* put back extension */
- if(ptr != 0) *ptr = '.';
- print_find_t(curdir, &find);
- }
- }
-
- if((find.F_ATTRIB & SUBDIR))
- search(curdir, find.F_NAME); /* recursion */
- } while( !F_NEXT( &find));
- }
- }
- /* display the file information */
- void print_find_t(char *dir, FILEINFO *find)
- {
- printf("%s%-12s %8ld %2.2d-%02.2d-%2.2d %c%c%c%c%c\r\n",
- dir, find->F_NAME, find->F_SIZE,
- (find->F_DATE >> 5) & 0x0f,
- find->F_DATE & 0x1f,
- ((find->F_DATE >> 9) + 80) % 100,
- (find->F_ATTRIB & SUBDIR) ? 'D' : '.',
- (find->F_ATTRIB & RDONLY) ? 'R' : '.',
- (find->F_ATTRIB & HIDDEN) ? 'H' : '.',
- (find->F_ATTRIB & SYSTEM) ? 'S' : '.',
- (find->F_ATTRIB & ARCH) ? 'A' : '.');
-
- found = 1;
- }
-
-